<template>
  <div class="home">
    <!-- 用户名:{{userIfo.user_info.account}} -->
    <bod>
      <!-- 侧边栏 -->
      <template v-slot:aside>
        <bods :dataList="userIfo.menus"> </bods>
      </template>
      <!-- 头部 -->
      <template class="top" v-slot:header>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item v-for="(v, i) in bread" :key="i">
            {{ v.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>查看</el-dropdown-item>
            <el-dropdown-item>新增</el-dropdown-item>
            <el-dropdown-item>删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span @mouseover="hg = true" @mouseout="hg = false">admin</span>
        <div
          class="box3"
          v-show="hg"
          @mouseover="hg = true"
          @mouseout="hg = false"
        >
          <ul>
            <li>个人中心</li>
            <li>
              <button @click="tc_dlFun" style="color: black">退出登录</button>
            </li>
          </ul>
        </div>
      </template>
      <!-- 主体 -->
      <template v-slot:main>
        <el-container style="height: 100%">
          <el-header>
            <div>
              <!-- tag导航 -->
              <el-tag
                :color="title.title == v.title ? 'pink' : ''"
                :key="i"
                v-for="(v, i) in tagInfo"
                closable
                :disable-transitions="false"
                @click="getTagValAsync(v)"
                @close="delTaginfoAsync(i)"
              >
                {{ v.title }}
              </el-tag>
            </div>
          </el-header>
          <el-main>
            <router-view> </router-view>
          </el-main>
        </el-container>
      </template>
    </bod>
  </div>
</template>

<script>
import bod from "@/components/bod.vue";
import { mapState, mapActions } from "vuex";
import { tcDl } from "../api/request.js";
export default {
  name: "Home",
  data() {
    return {
      inputVal: "",
      hg: false,
      breadList: [],
    };
  },
  // 解构
  computed: {
    ...mapState("user", ["userIfo"]),
    ...mapState("users", ["tagInfo", "title", "bread"]),
  },
  components: {
    bod,
    bods: () => import("@/components/bods.vue"),
  },
  methods: {
    // 删除头部导航
    ...mapActions("users", [
      "delTaginfoAsync",
      "getTagValAsync",
      "getBreadAsync",
    ]),
    //退出登录
    tc_dlFun() {
      this.$confirm("是否退出?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          tcDl().then((res) => {
            console.log(res);
            this.$message({
              type: "success",
              message: "退出成功!",
              duration: 200,
            });
            sessionStorage.clear();
            this.$router.push("/login");
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 面包屑导航
    breadFun(datas, val) {
      datas.forEach((v) => {
        if (
          v.title == val.title ||
          (v.children &&
            v.children.length &&
            JSON.stringify(v.children).indexOf(val.path) != -1)
        ) {
          this.breadList.push(v);
          if (
            v.children &&
            v.children.length &&
            JSON.stringify(v.children).indexOf(val.path) != -1
          ) {
            this.breadFun(v.children, val);
          }
        }
      });
    },
  },
  watch: {
    title(val) {
      //监听数据的变化
      this.breadList = [];
      this.breadFun(this.userIfo.menus, val);
      this.getBreadAsync(this.breadList);
      this.$router.push(val.path);
    },
  },
  created() {
    this.breadList = [];
    this.breadFun(this.userIfo.menus, this.title);
    this.getBreadAsync(this.breadList);
  },
};
</script>
<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
input {
  border: none;
  outline: none;
}
button {
  border: none;
  background: white;
}
.el-tag {
  margin: 0 10px;
}
</style>
